import cn.edu.zut.cs.Business.SaleService;
import cn.edu.zut.cs.Model.Info;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

import static org.springframework.web.context.support.SpringBeanAutowiringSupport.processInjectionBasedOnServletContext;

@WebServlet(urlPatterns = "/HelloServlet")
public class HelloServlet extends HttpServlet {

    SaleService service;
    Info info;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        processInjectionBasedOnServletContext(this, config.getServletContext());
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String health = request.getParameter("health");
        String date = request.getParameter("date");
        PrintWriter out = response.getWriter();

        info.setId(id);
        info.setName(name);
        info.setHealth(health);
        info.setDate(date);

        service.insert(info);
        out.println(id);
        out.println(name);
        out.println(health);
        out.println(date);


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("From Servlet!");
    }
    @Autowired
    public void setService(SaleService service) {
        this.service = service;
    }
    @Autowired
    public void setInfo(Info info) {
        this.info = info;
    }
}
